package Controller;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import Model.Encryption;


public class Server 
{
	private int m_Port; 
	
	public Server(int port)
	{
		m_Port = port;
	}
	
	public void start() 
	{	
		ServerSocket serverSocket = null;
		Socket connSocket = null;
		
		String key = null;
		switch (m_Port) 
		{
			case 3333:
				key = "Bar12345Bar12345";
				break;
			case 4444:
				key = "Bar12346Bar12345";		
				break;
			case 5555:
				key = "Bar12347Bar12345";
				break;
			case 6666:
				key = "Bar12348Bar12345";
				break;

			default:
				break;
		}
		
		Encryption enc = new Encryption(key);
		
		try 
		{
	        System.out.println("starting mix at: " + m_Port);
			
	        // start listening
			serverSocket = new ServerSocket(m_Port);
			
			while (true)
			{
				// accept connections
		        connSocket = serverSocket.accept();
		         
		        // create separate thread for each request
		        Request request = new Request(connSocket, enc);
		        request.start();
			}
		}
        catch(Exception e) 
        {
        	
        }
		finally
		{
			try 
			{
				connSocket.close();
			} 
			catch (IOException e) 
			{
				
			}
			
			try 
			{
				serverSocket.close();
			} 
			catch (IOException e) 
			{
				
			}			
		}
	}
}
